package org.leviatan.dataharbour.gui.swingcomponents.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.listeners.DataSourceListListener;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlDataSource;
import org.leviatan.definator.core.model.Table;

public class DataSourcesDefsPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4948042784627610495L;
	
	
	private JLabel lblTitle;
	
	private JList defsList;
	
	private JScrollPane defsListScroll;
	
	private JToolBar tBar;
	
	private JButton butAdd, butDelete;
	
	
	public DataSourcesDefsPanel() {
		super();
		initialize();
	}
	
	private void initialize() {
		
		lblTitle = new JLabel("Table definitions in selected datasource");
		defsList = new JList();
		defsListScroll = new JScrollPane(defsList);
		tBar = this.initToolBar();
		
		RuntimeContext.defsList = this.defsList;
		defsList.addListSelectionListener(new DataSourceListListener());
		
		
		///////////////////////////////////////////////////////////////////////
		
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints lblTitleC = new GridBagConstraints();
		GridBagConstraints defsListScrollC = new GridBagConstraints();
		GridBagConstraints tBarC = new GridBagConstraints();
		
		
		tBarC.anchor = GridBagConstraints.NORTHWEST;
		tBarC.fill = GridBagConstraints.BOTH;
		tBarC.gridx = 0;
		tBarC.gridy = 0;
		
		lblTitleC.anchor = GridBagConstraints.NORTHWEST;
		lblTitleC.fill = GridBagConstraints.BOTH;
		lblTitleC.insets = new Insets (10, 5,10,5);
		lblTitleC.gridx = 0;
		lblTitleC.gridy = 1;
		
		defsListScrollC.anchor = GridBagConstraints.NORTHWEST;
		defsListScrollC.insets = new Insets (10, 5, 10 ,5);
		defsListScrollC.fill = GridBagConstraints.BOTH;
		defsListScrollC.gridx = 0;
		defsListScrollC.gridy = 2;
		defsListScrollC.weighty = 1.0;
		defsListScrollC.weightx = 1.0;
		
		/*
		defsTreeC.anchor = GridBagConstraints.NORTHWEST;
		defsTreeC.gridx = 0;
		defsTreeC.gridy = 0;
		*/
		
		this.add(tBar, tBarC);
		this.add(lblTitle, lblTitleC);
		
		//this.add(lblDefsListTitle, lblDefsListTitleC);
		
		this.add(defsListScroll, defsListScrollC);
			
	}
	
	private JToolBar initToolBar() {
		
		JToolBar aBar = new JToolBar();
		aBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butCreateTblConstr = new GridBagConstraints();
		butCreateTblConstr.anchor = GridBagConstraints.WEST;
		butCreateTblConstr.insets = new Insets(0, 5, 0, 5);
		butCreateTblConstr.gridx = 0;
		butCreateTblConstr.gridy = 0;
		butCreateTblConstr.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteConstr = new GridBagConstraints();
		butDeleteConstr.anchor = GridBagConstraints.WEST;
		butDeleteConstr.insets = new Insets(0, 0, 0, 5);
		butDeleteConstr.gridx = 3;
		butDeleteConstr.gridy = 0;
		butDeleteConstr.weightx = 1.0;
		butDeleteConstr.weighty = 1.0;
		
		GridBagConstraints butCreateRecConstr = new GridBagConstraints();
		butCreateRecConstr.anchor = GridBagConstraints.WEST;
		butCreateRecConstr.insets = new Insets(0, 0, 0, 5);
		butCreateRecConstr.gridx = 1;
		butCreateRecConstr.gridy = 0;
		
		GridBagConstraints butCreateFldConstr = new GridBagConstraints();
		butCreateFldConstr.anchor = GridBagConstraints.WEST;
		butCreateFldConstr.insets = new Insets(0, 0, 0, 5);
		butCreateFldConstr.gridx = 2;
		butCreateFldConstr.gridy = 0;
		
		butAdd = new JButton("Add structure");
		butDelete = new JButton("Remove selected");
		
		butAdd.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String dsId = getSelectedDsId();
				if (dsId != null && !dsId.equals("")) {
					Collection dsDefs = 
						RuntimeContext.dataHarbourService.getDataSourcesDefs(dsId);
					
					Collection<Table> allDefs = 
						RuntimeContext.dataHarbourService.getAllVirtualStructures();
					
					Vector<Table> allDefsV = new Vector<Table>(allDefs);
					
					Vector<Table> allDefsCloned = (Vector<Table>) allDefsV.clone();
					
					if (dsDefs != null && !dsDefs.isEmpty()) {
						allDefsCloned.removeAll(dsDefs);
					}
					
					Object[] possibilities = allDefsCloned.toArray();
					Table t = (Table)JOptionPane.showInputDialog(
							RuntimeContext.defsList,
					        "Select table stucture to add to this datasource.",
					        "Select table definition",
					        JOptionPane.PLAIN_MESSAGE,
					        new ImageIcon(),
					        possibilities,
					        "");
	
					// String was returned
					if (t != null) {
						RuntimeContext.dataHarbourService.addDefToDataSource(dsId, String.valueOf(t.getId()));
						updateListData();
					}
	
					// If you're here, the return value was null/empty.
					//setLabel("Come on, finish the sentence!");
					
					RuntimeContext.dataHarbourService.saveDataSource2DefinitionsMappings();
				}
			}
		});
		
		butDelete.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				Table t = (Table) defsList.getSelectedValue();
				
				DefaultMutableTreeNode selectedDataSource = (DefaultMutableTreeNode) 
					RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

				if (selectedDataSource == null) return;
			
				TreeNodeMdlDataSource nodeModelDS = (TreeNodeMdlDataSource) 
					selectedDataSource.getUserObject();
				
				RuntimeContext.dataHarbourService.removeDefFromDataSource(
						nodeModelDS.getId(), String.valueOf(t.getId()));
				
				//RuntimeContext.dataHarbourService.saveRegister();
				
				RuntimeContext.dataHarbourService.saveDataSource2DefinitionsMappings();
				
				updateListData();
			}
		});
		
		aBar.add(butAdd, butCreateTblConstr);
		aBar.add(butDelete, butDeleteConstr);
		
		aBar.setRollover(true);
		aBar.setFloatable(false);
		
		return aBar;
		
	}
	
	private String getSelectedDsId() {
		DefaultMutableTreeNode selectedDataSource = (DefaultMutableTreeNode) 
			RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

		if (selectedDataSource == null) return null;

		TreeNodeMdlDataSource nodeModelDS = (TreeNodeMdlDataSource) 
			selectedDataSource.getUserObject();
		
		return nodeModelDS.getId();
	}
	
	private void updateListData() {
		if (this.getSelectedDsId() == null) return;
		
		Vector defs = RuntimeContext.dataHarbourService.getDataSourcesDefs(this.getSelectedDsId());
		if (defs != null)
			RuntimeContext.defsList.setListData(defs);
	}

}
